//
//  RootViewController.m
//  AppBook_No1
//
//  Created by lee sangseong on 11. 11. 7..
//  Copyright (c) 2011년 nexplatcorea. All rights reserved.
//

#import "RootViewController.h"
#import "MainViewController.h"
#import "GPSViewController.h"

@implementation RootViewController
@synthesize activity;
@synthesize beforeActivity;
static id instance_;

+ (id)sharedContorller
{
    if ( ! instance_ )
    {
        NSLog( @"Not Instance" );
        return nil;
    }
    return instance_;
}

- (void)dealloc
{
    [activity release];
    activity = nil;
    
    [beforeActivity release];
    beforeActivity = nil;
    
    [super dealloc];
}

- (id)init
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    [view setUserInteractionEnabled:YES];
    [view setAutoresizesSubviews:NO];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self setView:view];
    [view release];
    
    instance_ = self;
    
    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent animated:YES];
    
    Activity *activity_temp;
    activity_temp = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:[NSBundle mainBundle]];
    self.activity = activity_temp;
    self.activity.view.backgroundColor = [UIColor clearColor];
    
    [activity_temp release];
    activity_temp = nil;
    
    [self.view addSubview:activity.view];
    
    viewIndex = VIEW_INDEX_MAIN;
    
    return self;
}

- (void)changeView:(VIEW_INDEX)index animation:(ANIMATION_INDEX)animation
{
    beforeIndex = viewIndex;
    viewIndex = index;
    
    if(activity != nil)
    {
        [activity.view removeFromSuperview];
    }
    
    Activity *activity_temp = nil;
    
    switch (viewIndex)
    {
        case VIEW_INDEX_NONE:
            break;
        case VIEW_INDEX_MAIN:
            activity_temp = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:[NSBundle mainBundle]];
            break;
        case VIEW_INDEX_GPS:
            activity_temp = [[GPSViewController alloc] initWithNibName:@"GPSViewController" bundle:[NSBundle mainBundle]];
            break;
        //Add Your View Index
        default:
            break;
    }
    
    switch (animation)
    {
        case ANIMATION_INDEX_NONE:
            break;
        case ANIMATION_INDEX_FLIP:
            NSLog(@"Flip Animation");
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.8];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
            [self.activity viewWillDisappear:YES];
            [activity_temp viewWillAppear:YES];
            [UIView commitAnimations];
            break;
        case ANIMATION_INDEX_ALPHA:
            NSLog(@"Alpha Animation");
            [activity_temp.view setAlpha:0.0f];
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.8];
            [activity_temp.view setAlpha:1.0f];
            [self.activity.view setAlpha:0.0f];
            [UIView commitAnimations];
            break;
            
        default:
            break;
    }
    
    @try {
        if(![activity_temp isEqual:[NSNull null]])
        {
            [activity_temp.view setBackgroundColor:[UIColor blackColor]];
            self.beforeActivity = self.activity;
            self.activity = activity_temp;
            [self.view addSubview:activity.view];
            [activity viewDidAppear:YES];
            [activity_temp release];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }
    @finally {
        
    }
}

#pragma mark - Getter Function
- (Activity*)getCurrentActivity
{
    return activity;
}

- (Activity*)getBeforeActivity
{
    return  beforeActivity;
}

#pragma mark - View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
